package com.dletc.dao.Impl;

import com.dletc.dao.OrderDao;
import com.dletc.domain.Dish;
import com.dletc.domain.Order;
import com.dletc.utils.DBUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

public class OrderDaoImpl implements OrderDao {

    //增加订单
    @Override
    public void AddOrderByUser(Order order) {
        int count = 0;
        try {
            QueryRunner qr = new QueryRunner(DBUtils.getDataSource());

            //先插入数据到Order_User表
            String sql_Order_User = "insert into Order_User values(null,?,?,?)";
            count = qr.update(sql_Order_User, order.getUserID(), order.getIsDown(), order.getOrderTime());
            System.out.println(count > 0?"订单添加成功":"订单添加失败");

            //获取Order_User表中刚插入的UserID
            String sql3 = "select * from Order_User where OrderTime = ? ";
            Order query = qr.query(sql3, new BeanHandler<Order>(Order.class),order.getOrderTime());
            order.setOrderID(query.getOrderID());

            //再插入数据到OrderDish
            String sql_OrderDish = "insert into OrderDish values(?,?)";
            for (Dish dish : order.getList()) {
                int count2 = qr.update(sql_OrderDish, order.getOrderID(), dish.getDishID());
                System.out.println(count2 > 0?"订单菜品添加成功":"订单菜品添加失败");
            }


        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

    }

    //商家获取所有订单
    @Override
    public List<Order> GetAllOrder() {

        QueryRunner qr = new QueryRunner(DBUtils.getDataSource());
        List<Order> list = null;
        try {
            //一个订单有多个菜品
            //先查询订单表Order_User
            String sql_OU = "select * from Order_User";

            list = qr.query(sql_OU, new BeanListHandler<Order>(Order.class));

            //遍历集合依次获取当前订单的OrderID
            for (Order order : list) {
                int orderID = order.getOrderID();

                //用遍历到的当前订单ID 到OrderDish表中查每个订单订的菜
                String sql_OD = "select od.DishID ,d.DishName ,d.DishMoney from OrderDish od INNER JOIN Dish d on od.DishID = d.DishID  where OrderID = ?";
                List<Dish> order_get = qr.query(sql_OD, new BeanListHandler<Dish>(Dish.class), orderID);
                order.setList(order_get);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return list;
    }

    //顾客查询自己的所有订单
    @Override
    public List<Order> SelectByUserID(Order order) {
        QueryRunner qr = new QueryRunner(DBUtils.getDataSource());
        List<Order> list = null;

        try {
            //通过User_ID查当前用户的所有订单
            String sql_OU = "select * from Order_User where UserID = ?";

            list = qr.query(sql_OU, new BeanListHandler<Order>(Order.class),order.getUserID());

            for (Order order_list : list) {
                int orderID = order_list.getOrderID();

                //用遍历到的当前订单ID 到OrderDish表中查每个订单订的菜
                String sql_OD = "select od.DishID ,d.DishName ,d.DishMoney from OrderDish od INNER JOIN Dish d on od.DishID = d.DishID  where OrderID = ?";
                List<Dish> query = qr.query(sql_OD, new BeanListHandler<Dish>(Dish.class),orderID);
                order_list.setList(query);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

        return list;
    }

    //查看订单状态,返回order表里的isdown
    @Override
    public int GetOrderIsDown(int OrderID) {
        QueryRunner qr = new QueryRunner(DBUtils.getDataSource());

        int isDown = 0;
        try {
            String sql = "select * from Order_User where OrderID = ?";

            Order order = qr.query(sql, new BeanHandler<Order>(Order.class), OrderID);
            isDown = order.getIsDown();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return isDown;
    }

    //修改订单状态操作(订单是否完成)
    @Override
    public void UpdateIsDown(int OrderID, int IsDown) {
        QueryRunner qr = new QueryRunner(DBUtils.getDataSource());

        int count = 0;
        try {
            String sql = "update Order_User set IsDown = ? where OrderID = ?";

            count = qr.update(sql, IsDown, OrderID);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

        System.out.println(count > 0?"订单状态修改成功":"订单状态修改失败");
    }
}
